var yzPhone;
var yzCode;

// 获取验证码
function getCode(reg) {
    var phone = $.trim($('.phone input').val());
    if (!checkMobile(phone)) {
        _msg('手机号不正确');
        _log('手机号不正确');
        return;
    }
    var status = $('.get-code').attr('data-status');
    if (status != 0) return;

    var code = getRandom(1000, 9999);
    // 判断是否注册
    if (reg == 1) {

        _ajax('home/user/sel', function (ret, err) {
            if (ret.code != 200) {
                _msg('手机号已经被注册');
            } else {
                _ajax('api/code/sms', function(rets){
                    yzPhone = phone;
                    yzCode = code;
                    _msg('发送成功');
                    _log('发送成功')
                    downTime();
                }, {
                    phone: phone,
                    code: code,
                })
            }
        }, {
            phone: phone
        })
    } else {
        _ajax('api/code/sms', function(rets){
            yzPhone = phone;
            yzCode = code;
            _msg('发送成功');
            _log('发送成功')
            downTime();
        }, {
            phone: phone,
            code: code,
        })
    }
}

// 校验验证码是否正确
function checkCode(phone, code, callback) {
    if (phone != yzPhone || code != yzCode) {
        _msg('验证码不正确');
        return;
    }
    if (typeof callback == 'function') {
        callback();
    }
}

// 倒计时
function downTime() {
    var i = 60;
    $('.get-code').attr('data-status', 1);
    var time = setInterval(function () {
        i--;
        if (i == 0) {
            clearInterval(time);
            $('.get-code').attr('data-status', 0)
            $('.get-code').text('重新获取');
            return;
        }
        $('.get-code').text('请稍后' + i + 's');
    }, 1000)
}